/*企业账号管理-列表*/
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:frametrim/route/Routes.dart';
import 'package:frametrim/views/background_view.dart';
import 'package:frametrim/views/button_round.dart';
import 'package:frametrim/views/button_round_single.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../utility/sq_color.dart';
import '../../views/MyUnderlineTabIndicator.dart';
import '../../views/appbar_search_toAppbar.dart';
import '../../views/dialogs/commonDialog.dart';

class CompanyAccountManagePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => CompanyAccountManageState();
}

class CompanyAccountManageState extends State<CompanyAccountManagePage>
    with TickerProviderStateMixin {
  TextEditingController _controller = TextEditingController();

  final List<Tab> myTabs = [
    Tab(text: '全部'),
    Tab(text: '启用'),
    Tab(text: '待审核'),
    Tab(text: '审核退回'),
    Tab(text: '停用')
  ];

  final List<String> mStatus = ['审核通过', '审核退回', '待审核', '停用'];

  late TabController _tabController; //需要定义一个Controller

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    // 创建Controller
    _tabController = TabController(length: myTabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: SQColor.color_background,
        appBar: AppBar(
          backgroundColor: Colors.white,
          titleSpacing: 0,
          elevation: 0,
          title: AppbarSearchUseAppBarTitle(
            imgStr: 'image/imv_back.png',
            controller: _controller,
            callback: (str) {},
            isNeedJump: true,
            isShowSearchImg: false,
          ),
          leadingWidth: 0,
          bottom: TabBar(
            controller: _tabController,
            tabs: myTabs,
            // 定义TabWeight,若数量和定义不一致会报错
            onTap: (int index) {
              print('Selected......$index');
            },
            //设置未选中时的字体颜色，tabs里面的字体样式优先级最高
            unselectedLabelColor: SQColor.color_90black,
            //设置未选中时的字体样式，tabs里面的字体样式优先级最高
            unselectedLabelStyle: TextStyle(fontSize: 16),
            //设置选中时的字体颜色，tabs里面的字体样式优先级最高
            labelColor: SQColor.color_90black,
            //设置选中时的字体样式，tabs里面的字体样式优先级最高
            labelStyle: TextStyle(fontSize: 16.0),
            //允许左右滚动
            isScrollable: true,
            indicator: MyUnderlineTabIndicator(
                borderSide:
                    BorderSide(width: 3.0, color: SQColor.color_0052D9)),
          ),
        ),
        body: Column(
          children: [
            Divider(height: 1, color: SQColor.color_DEDEDE),
            Expanded(
                child: SmartRefresher(
                    enablePullDown: true,
                    enablePullUp: true,
                    controller: _refreshController,
                    onRefresh: () async {
                      Future.delayed(Duration(seconds: 1), () async {
                        _refreshController.refreshCompleted();
                      });
                    },
                    onLoading: () async {
                      Future.delayed(Duration(seconds: 1), () async {
                        _refreshController.loadComplete();
                      });
                    },
                    child: ListView.builder(
                      shrinkWrap: true,
                      itemBuilder: (context, index) {
                        return buildItem(index);
                      },
                      itemCount: mStatus.length,
                    )))
          ],
        ));
  }

  /*item*/
  Widget buildItem(int index) {
    return GestureDetector(
      onTap: (){
        Get.toNamed(Routes.Company_Account_Manage_Details);
      },
      child: BackgroundView(
          mVerticalTopMargin: 10,
          mVerticalBottomMargin: 0,
          mHorizontalMargin: 12,
          mHorizontalPadding: 10,
          mVerticalPadding: 10,
          widget: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Expanded(
                      child: Text('郑州众一建筑劳务分包有限公司',
                          style: TextStyle(
                              fontSize: 16, color: SQColor.color_3D3D3D))),
                  Text(mStatus[index],
                      style: TextStyle(fontSize: 14, color: getColor(index)))
                ],
              ),
              SizedBox(height: 24),
              Text('企业类型：类型1',
                  style: TextStyle(fontSize: 14, color: SQColor.color_3D3D3D)),
              SizedBox(height: 5),
              Text('统一社会信用代码：：123456789001212121',
                  style: TextStyle(fontSize: 14, color: SQColor.color_3D3D3D)),
              SizedBox(height: 15),
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: buildbutton(index),
                ),
              )
            ],
          )),
    );
  }

  /*底部按钮*/
  List<Widget> buildbutton(int index) {
    List<Widget> children = [];
    if (mStatus[index] == '审核通过') {
      children.add(ButtonRoundSingleView(
          mHorizontalPadding: 16,
          mVerticalPadding: 8,
          textStr: '重置密码',
          textColor: SQColor.color_90black,
          textSize: 14,
          buttonColor: SQColor.color_C5C5C5,
          onPress: () {}));
      children.add(SizedBox(width: 15));
      children.add(ButtonRoundView(
          mHorizontalPadding: 16,
          mVerticalPadding: 7,
          textStr: '停用账号',
          textColor: SQColor.white,
          textSize: 14,
          buttonColor: SQColor.color_0052D9,
          onPress: () {
            StopUseDialog();
          }));
    }else if(mStatus[index] == '审核退回'||mStatus[index] == '待审核'){
      children.add(ButtonRoundView(
          mHorizontalPadding: 16,
          mVerticalPadding: 7,
          textStr: '删除',
          textColor: SQColor.white,
          textSize: 14,
          buttonColor: SQColor.color_0052D9,
          onPress: () {
            DelDialog();
          }));
    }else if(mStatus[index] == '停用'){
      children.add(ButtonRoundSingleView(
          mHorizontalPadding: 16,
          mVerticalPadding: 8,
          textStr: '重置密码',
          textColor: SQColor.color_90black,
          textSize: 14,
          buttonColor: SQColor.color_C5C5C5,
          onPress: () {}));
      children.add(SizedBox(width: 15));
      children.add(ButtonRoundView(
          mHorizontalPadding: 16,
          mVerticalPadding: 7,
          textStr: '启用账号',
          textColor: SQColor.white,
          textSize: 14,
          buttonColor: SQColor.color_0052D9,
          onPress: () {
            StartUseDialog();
          }));
    }
    return children;
  }

  /*获取状态颜色*/
  Color getColor(int index) {
    Color textColor = SQColor.color_00A870;
    if (mStatus[index] == '审核通过') {
      textColor = SQColor.color_00A870;
    } else if (mStatus[index] == '审核退回') {
      textColor = SQColor.color_E34D59;
    } else if (mStatus[index] == '待审核') {
      textColor = SQColor.color_ED7B2F;
    } else if (mStatus[index] == '停用') {
      textColor = SQColor.color_60black;
    }

    return textColor;
  }

  /*停用账号弹窗*/
  void StopUseDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext dialogContext) {
        return CommonDialog(
          title: '确认停用当前企业账号吗？',
          content: '停用账号后当前账号不可使用。若更新账号信息，可以操作停用后，再次发起企业账号认证。',
          button_left: '取消',
          onPress: () {

          },
        );
      },
    );
  }
  /*启用账号弹窗*/
  void StartUseDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext dialogContext) {
        return CommonDialog(
          title: '确认启用当前企业账号吗？',
          content: '确认启用当前企业账号吗？',
          button_left: '取消',
          onPress: () {

          },
        );
      },
    );
  }
  /*重置密码弹窗*/
  void ResetPswDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext dialogContext) {
        return CommonDialog(
          title: '确认重置当前企业账号密码吗？',
          content: '确认重置当前企业账号密码吗？',
          button_left: '取消',
          onPress: () {

          },
        );
      },
    );
  }
  /*删除弹窗*/
  void DelDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext dialogContext) {
        return CommonDialog(
          title: '确认重置当前企业账号密码吗？',
          content: '确认重置当前企业账号密码吗？',
          button_left: '取消',
          button_rightColor: SQColor.color_E34D59,
          onPress: () {

          },
        );
      },
    );
  }
}
